package com.backstage.userModule.controller;


import com.backstage.userModule.pojo.User;
import com.backstage.userModule.service.UserService;
import com.backstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 移动端用户表 前端控制器
 * </p>
 *
 * @author liu-xiang
 * @since 2022-12-13
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
  @Autowired
  private UserService userService;

  @PostMapping("/list/{name}/{phone}/{page}/{size}")
  public R list(@PathVariable String name, @PathVariable String phone,
                @PathVariable Integer page, @PathVariable Integer size) {
    if (name == null || name.replace(" ", "").equals("") || name.equals("null"))
      name = null;
    if (phone == null || phone.replace(" ", "").equals("") || phone.equals("null"))
      phone = null;
    if (page == null || page == 0)
      page = 1;
    if (size == null || size == 0)
      page = 10;

    List<User> userList = userService.list(name, phone, page, size);
    if (userList == null) return R.error();

    return R.ok().data("userList", userList);
  }

  @PostMapping("/list/{page}/{size}")
  public R list(@PathVariable Integer page, @PathVariable Integer size) {
    return R.ok().data("userList", this.list(null,null,page,size));
  }

  @PostMapping("add")
  public R add(@RequestBody User user) {
    return userService.save(user) ? R.ok() : R.error();
  }

  @DeleteMapping("delete/{uid}")
  public R delete(@PathVariable String uid) {
    return userService.removeByUid(uid) ? R.ok() : R.error();
  }

  @PostMapping("update")
  public R update(@RequestBody User user) {
    return userService.updateExists(user) ? R.ok() : R.error();
  }
}

